/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: ---Technology Co. Ltd</p>
 * <p>2014-4-29</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.mybatis;

import java.beans.PropertyVetoException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.DataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;


/** 
 * desc: C3P0 DataSouce Implement Class
 * <p>author: Jan.Zhang </p>
 * date: 2014-4-29
 */
public class CSC3P0DataSourceFactory implements DataSourceFactory{
	
//private final static Logger logger = LoggerFactory.getLogger(CSC3P0DataSourceFactory.class);
	
	private DataSource dataSource;
	
	@Override
	public void setProperties(Properties props) {
		ComboPooledDataSource cpds = new ComboPooledDataSource();
		String driverClass = props.getProperty("driverClass");
		try {
			cpds.setDriverClass(driverClass);
		} catch (PropertyVetoException e) {
//			logger.error("driverClass load error " + driverClass, e);
			throw new IllegalArgumentException("driverClass\u4E0D\u6B63\u786E\uFF01");
		}
		
		cpds.setJdbcUrl(props.getProperty("jdbcUrl"));
		cpds.setUser(props.getProperty("user"));
		cpds.setPassword(props.getProperty("password"));
		cpds.setInitialPoolSize(Integer.parseInt(props.getProperty("initialPoolSize")));
		cpds.setMinPoolSize(Integer.parseInt(props.getProperty("minPoolSize")));
		cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("maxPoolSize")));
		cpds.setAcquireIncrement(Integer.parseInt(props.getProperty("maxPoolSize")));
		cpds.setIdleConnectionTestPeriod(Integer.parseInt(props.getProperty("maxPoolSize")));
		cpds.setMaxIdleTime(Integer.parseInt(props.getProperty("maxPoolSize")));
		dataSource = cpds;
		
	}

	@Override
	public DataSource getDataSource() {
		return dataSource;
	}
}
